package mbp.gui.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;

import mbp.base.MessageType;
import mbp.core.Service;
import mbp.gui.frames.MbpManagerGui;
import mbp.gui.frames.ServiceEditor;
import mbp.message.MessageXml;

public class ServiceViewerGui extends JPanel  implements ActionListener
{
	private MbpManagerGui mbpManagerGui;
	private JTextField txtAgentName;
	private JButton btnGet;
	
	private ServiceListPanel srvList;

	/**
	 * Create the panel.
	 */
	public ServiceViewerGui(MbpManagerGui mbpManagerGui)  
	{
		this.mbpManagerGui = mbpManagerGui;
		setLayout(null);
		
		JPanel panel_0 = new JPanel();
		panel_0.setLayout(null);
		panel_0.setBorder(new TitledBorder(null, "Agent", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_0.setBounds(12, 12, 432, 69);
		add(panel_0);
		
		txtAgentName = new JTextField();
		txtAgentName.setColumns(10);
		txtAgentName.setBounds(10, 38, 120, 20);
		panel_0.add(txtAgentName);
		
		JLabel label = new JLabel("Agent name");
		label.setBounds(10, 21, 100, 14);
		panel_0.add(label);
		
		btnGet = new JButton("Get");
		btnGet.setBounds(140, 35, 91, 23);
		panel_0.add(btnGet);
		btnGet.addActionListener(this);
		
		srvList = new ServiceListPanel() {
			public void processDoubleClick(int index)
			{
				getDetailsSrv(index);
			}
		};
		srvList.setBorder(new TitledBorder(null, "Services list", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		srvList.setBounds(12, 93, 432, 233);
		add(srvList);

	}
	
	public void getDetailsSrv(int index)
	{
		mbpManagerGui.myAgent.SendServiceGet(this.txtAgentName.getText(), index);
	}
	
	public synchronized void reloadServicesList(MessageXml messageXml)
	{
		if(mbpManagerGui == null)return;
		srvList.reloadListInfo(messageXml.services);		
	}
	
	public void loadService(Service srv)
	{
		ServiceEditor serviceEditor = new ServiceEditor(srv);
		serviceEditor.setVisible(true);
	}
	

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == btnGet)
		{
			mbpManagerGui.myAgent.SendObjectLGet(this.txtAgentName.getText(), MessageType.SERVICELGET);
		}
		
	}
	
	
}
